package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.dao.SaleListGoodsDao;
import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * ClassName: SaleListGoodsServiceImpl
 * Description:
 * Date: 2022/1/17 21:11
 *
 * @version:
 * @since JDK 1.8
 */
@Service
public class SaleListGoodsServiceImpl implements SaleListGoodsService {

    @Autowired
    private SaleListGoodsDao saleListGoodsDao;

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private GoodsTypeDao goodsTypeDao;

    @Override
    public void saveSaleListGoods(SaleList saleList, String saleListGoodsStr, HttpSession session) {


        User user = (User)session.getAttribute("currentUser");

        saleList.setUserId(user.getUserId());
        saleListGoodsDao.saveSaleList(saleList);

        List<SaleListGoods> saleListGoodsList = JSONObject.parseArray(saleListGoodsStr, SaleListGoods.class);

        for (SaleListGoods saleListGoods : saleListGoodsList) {
            saleListGoods.setSaleListId(saleList.getSaleListId());
            saleListGoodsDao.saveSaleListGoods(saleListGoods);

        }






    }

    @Override
    public List<SaleList> getSaleListGoods(String saleNumber, Integer customerId, Integer state, String sTime, String eTime) {
        List<SaleList> saleListList = saleListGoodsDao.getSaleListGoods(saleNumber,customerId,state,sTime,eTime);
        return saleListList;
    }

    @Override
    public List<SaleListGoods> getGoodsList(Integer saleListId) {
        List<SaleListGoods>saleListGoodsList = saleListGoodsDao.getGoodsList(saleListId);
        return saleListGoodsList;
    }

    @Override
    public void deleteSaleListGoods(Integer saleListId) {
        saleListGoodsDao.deleteSaleListGoods(saleListId);
        saleListGoodsDao.deleteSaleList(saleListId);
    }


    @Override
    public void updateState(Integer saleListId) {
        saleListGoodsDao.updateState(saleListId);
    }


    @Override
    public List<saleListGoodsCount> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {

            List<Integer> listGoodsIds = saleListGoodsDao.getGoodsList1(goodsTypeId,codeOrName);

            List<SaleList> SaleListList= saleListGoodsDao.getSaleListGoods(null, null,null,sTime,eTime);
            ArrayList<saleListGoodsCount> saleListGoodsCountArrayList = new ArrayList<>();
            for (SaleList saleList : SaleListList) {
                String customerName = customerDao.selectById1(saleList.getCustomerId());
                List<SaleListGoods> SaleListGoodsList = saleListGoodsDao.getGoodsList(saleList.getSaleListId());
                for (SaleListGoods saleListGoods : SaleListGoodsList) {

                    String goodsTypeName = goodsTypeDao.selectById(saleListGoods.getGoodsTypeId());

                    saleListGoodsCount saleListGoodsCount = new saleListGoodsCount();

                    saleListGoodsCount.setPurchaseListGoodsId(saleListGoods.getSaleListGoodsId());
                    saleListGoodsCount.setNumber(saleList.getSaleNumber());
                    saleListGoodsCount.setCustomerName(customerName);
                    saleListGoodsCount.setDate(saleList.getSaleDate());
                    saleListGoodsCount.setCode(saleListGoods.getGoodsCode());
                    saleListGoodsCount.setName(saleListGoods.getGoodsName());
                    saleListGoodsCount.setModel(saleListGoods.getGoodsModel());
                    saleListGoodsCount.setGoodsType(goodsTypeName);
                    saleListGoodsCount.setUnit(saleListGoods.getGoodsUnit());
                    saleListGoodsCount.setPrice(saleListGoods.getPrice());
                    saleListGoodsCount.setNum(saleListGoods.getGoodsNum());
                    saleListGoodsCount.setTotal(saleListGoods.getTotal());
                    saleListGoodsCountArrayList.add(saleListGoodsCount);
                }
            }


            ArrayList<saleListGoodsCount> purchaseListGoodsCountArrayList1 = new ArrayList<>();
            for (Integer listGoodsId : listGoodsIds) {
                for (saleListGoodsCount saleListGoodsCount : saleListGoodsCountArrayList) {
                    if(listGoodsId.equals(saleListGoodsCount.getPurchaseListGoodsId())){
                        purchaseListGoodsCountArrayList1.add(saleListGoodsCount);
                    }
                }
            }
            return purchaseListGoodsCountArrayList1;

    }


    @Override
    public List<SaleDataByDay> getSaleDataByDay(String sTime, String eTime) {



        List<SaleDataByDay> SaleDataByDay =  saleListGoodsDao.getSaleDataByDay( sTime,  eTime);
        return SaleDataByDay;
    }


    @Override
    public List<SaleDataByDay> getSaleDataByMonth(String sTime, String eTime) {
        List<SaleDataByDay> saleDataByMonth =  saleListGoodsDao.getSaleDataByMonth( sTime,  eTime);
        return saleDataByMonth;
    }
}
